package REDES2;


import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/*
 * Colors.java
 *
 * Created on 22 de Junho de 2009, 07:57
 */



/**
 *
 * @author  Administrador
 */
public class Colors extends javax.swing.JFrame {
    static ObjectOutputStream out;
    static String source, destination, nome;
            
    public Colors(String name, ObjectOutputStream outp, String sourc, String dest) {
        initComponents();
        out = outp;
        source = sourc;
        destination = dest;
        nome = name;
        setTitle("Escolha as cores : "+nome);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jbtblue = new javax.swing.JButton();
        jbtgreen = new javax.swing.JButton();
        jbtred = new javax.swing.JButton();
        jbtyellow = new javax.swing.JButton();
        jbsend = new javax.swing.JButton();
        jbtzerar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Escolha as cores");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));

        jbtblue.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/blue.JPG"))); // NOI18N
        jbtblue.setActionCommand("blue");
        jbtblue.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/bluieroll.jpg"))); // NOI18N
        jbtblue.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/blueselected.jpg"))); // NOI18N
        jbtblue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtblueActionPerformed(evt);
            }
        });

        jbtgreen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/green.JPG"))); // NOI18N
        jbtgreen.setActionCommand("green");
        jbtgreen.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/greenpress.jpg"))); // NOI18N
        jbtgreen.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/greenroll.jpg"))); // NOI18N
        jbtgreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtgreenActionPerformed(evt);
            }
        });

        jbtred.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/red.JPG"))); // NOI18N
        jbtred.setActionCommand("red");
        jbtred.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/redroll.jpg"))); // NOI18N
        jbtred.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/redpress.jpg"))); // NOI18N
        jbtred.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtredActionPerformed(evt);
            }
        });

        jbtyellow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/yellow.JPG"))); // NOI18N
        jbtyellow.setActionCommand("yellow");
        jbtyellow.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/yellowpressed.jpg"))); // NOI18N
        jbtyellow.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/imgs/yellowroll.jpg"))); // NOI18N
        jbtyellow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtyellowActionPerformed(evt);
            }
        });

        jbsend.setText("ENVIAR");
        jbsend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbsendActionPerformed(evt);
            }
        });

        jbtzerar.setText("ZERAR");
        jbtzerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtzerarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jbtblue, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtgreen, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtred, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtyellow, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jbtzerar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbsend)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(29, 29, 29)
                        .addComponent(jLabel2)
                        .addGap(27, 27, 27)
                        .addComponent(jLabel3)
                        .addGap(26, 26, 26)
                        .addComponent(jLabel4)))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtgreen, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtblue, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtred, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtyellow, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jbsend)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtzerar)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        

    private void jbtzerarActionPerformed(java.awt.event.ActionEvent evt) {                                         
     jLabel1.setText("");jLabel2.setText("");jLabel3.setText("");jLabel4.setText("");
     for (int i = 0; i < colors.length; i++) {
			colors[i] = null;
		} 
     colorcounter = 0;
     jbtblue.setEnabled(true); jbtgreen.setEnabled(true); jbtred.setEnabled(true); jbtyellow.setEnabled(true);
     
}                                        
   
    private void jbsendActionPerformed(java.awt.event.ActionEvent evt) {                                       
        
      if (colorcounter == 4){
          for (int i = 0; i < colors.length; i++) {
              String string = colors[i];
              System.out.println(string);
          }
          System.out.println("counter:"+colorcounter);   
          sendPattern(colors);
          dispose();
      }
      else 
          JOptionPane.showMessageDialog(Colors.this, "Escolha 4 cores", "Error", JOptionPane.ERROR_MESSAGE);
}                                      

    private void jbtblueActionPerformed(java.awt.event.ActionEvent evt) {                                        
        setPattern("blue");    JButton b = (JButton)evt.getSource(); b.setEnabled(false);
}                                       

    private void jbtgreenActionPerformed(java.awt.event.ActionEvent evt) {                                         
         setPattern("green");  JButton b = (JButton)evt.getSource(); b.setEnabled(false);
}                                        

    private void jbtredActionPerformed(java.awt.event.ActionEvent evt) {                                       
         setPattern("red");    JButton b = (JButton)evt.getSource(); b.setEnabled(false);
}                                      

    private void jbtyellowActionPerformed(java.awt.event.ActionEvent evt) {                                          
         setPattern("yellow"); JButton b = (JButton)evt.getSource(); b.setEnabled(false);
}                                         
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Colors(null,null,null,null).setVisible(true);
            }
        });
    }
    
    protected void setPattern(String color) {
        colors[colorcounter] = color;
        colorcounter++;
        jLabel1.setText(colors[0]);
        jLabel2.setText(colors[1]);
        jLabel3.setText(colors[2]); 
        jLabel4.setText(colors[3]);
    }
    
     protected static  void sendPattern(String[] color) {
       String pattern ="";
        if (color[3] != null) {
            try {
                for (int i = 0; i < color.length; i++) {
                    pattern += color[i] + "/";
                }
                out.writeObject("%pat%"+"<"+source+">"+"<<"+destination+">>" + pattern);
            } catch (IOException ex) { System.out.println("erro ao enviar padrao das cores");}
        }
    }
     protected static  void sendAttempt(String[] color, ObjectOutputStream outp, String dest) {
       String pattern ="";
        if (color[3] != null) {
            try {
                for (int i = 0; i < color.length; i++) {
                    pattern += color[i] + "/";
                }
                outp.writeObject("(try)"+"<<"+dest+">>" + pattern);
            } catch (IOException ex) { System.out.println("erro ao enviar padrao das cores");}
        }
    }
    protected static String[] colors = new String[4];
    protected static int colorcounter = 0;
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    protected javax.swing.JButton jbsend;
    protected javax.swing.JButton jbtblue;
    protected javax.swing.JButton jbtgreen;
    protected javax.swing.JButton jbtred;
    protected javax.swing.JButton jbtyellow;
    protected javax.swing.JButton jbtzerar;
    // End of variables declaration                   
    
}
